package com.saic.localfileloader.main;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

public class LatLng {

	public static void main(String args[]) throws Exception {

		BufferedReader br = new BufferedReader(new InputStreamReader(
				new FileInputStream("test.TXT")));
		String line = null;
		while (null != (line = br.readLine())) {
			String[] tmp = line.split("\\|");
			for (int i = 0; i < 10; i++)
				System.out.print(tmp[i] + "\t");
			System.out.println();
		}
		br.close();

	}

	/**
	 * @DESC: 读取数据，筛选数据，过滤异常值
	 * @Data:1--正常;2--无法判断;3--异常值
	 * @throws Exception
	 */
	public static void filter() throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(
				new FileInputStream("data.txt")));
		// 假设已经按照VIN号&日期排序
		String line = null;
		String last = null;
		String current = null;
		List<String> list = new ArrayList<String>();// True Point
		while (null != (line = br.readLine())) {
			if (checkInvalidate(line.split("\\s+")[0], line.split("\\s+")[1]))
				continue;
			if (null == last) {
				last = line;
				continue;
			}
			if (null == current) {
				current = line;
				continue;
			}
			if (checkPoint(last, current, line)) {
				list.add(current);
			}
			last = current;
			current = line;
		}
		br.close();

		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
				new FileOutputStream("result.txt")));
		for (String s : list) {
			bw.write(s + "\n");
		}
		bw.flush();
		bw.close();
	}

	public static boolean checkPoint(String last, String current, String line) {

		double p1 = GetDistance(Double.parseDouble(last.split("\\s+")[0]),
				Double.parseDouble(last.split("\\s+")[1]),
				Double.parseDouble(current.split("\\s+")[0]),
				Double.parseDouble(current.split("\\s+")[1]));
		double p2 = GetDistance(Double.parseDouble(line.split("\\s+")[0]),
				Double.parseDouble(line.split("\\s+")[1]),
				Double.parseDouble(current.split("\\s+")[0]),
				Double.parseDouble(current.split("\\s+")[1]));
		if (p1 < 0 || p2 < 0)
			return false;
		if (Math.abs(p1) < 150 & Math.abs(p2) < 150) {
			return true;
		}
		return false;
	}

	/**
	 * @DESC:检查是否为不合理数值
	 */
	public static boolean checkInvalidate(String latStr, String lngStr) {
		double lat = Double.parseDouble(latStr);
		double lng = Double.parseDouble(lngStr);
		if (lat > 35.0 & lat < 45.0 & lng > 110.0 & lng < 120.0)
			return false;
		return true;
	}

	/**
	 * @DESC: 转化为弧度
	 */
	public static double rad(Double d) {
		return d * Math.PI / 180.0;
	}

	/**
	 * @DESC: 根据经纬度，计算实际距离
	 */
	public static double GetDistance(Double lat1, Double lng1, Double lat2,
			Double lng2) {
		if ((Math.abs(lat1) > 90) || (Math.abs(lat2) > 90)) {
			return -1.0;
		}
		if ((Math.abs(lng1) > 180) || (Math.abs(lng2) > 180)) {
			return -1.0;
		}
		double radLat1 = rad(lat1);
		double radLat2 = rad(lat2);
		double a = radLat1 - radLat2;
		double b = rad(lng1) - rad(lng2);
		double s = 2.0 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2.0), 2)
				+ Math.cos(radLat1) * Math.cos(radLat2)
				* Math.pow(Math.sin(b / 2.0), 2)));
		s = s * 6378.137;
		return Math.round(s * 100000) * 1000.0 / 100000; // M
	}
}